

<script>
  import { querySupplier,getSupplier,updateSupplier,createSupplier} from '@/api/erp/supplier'

  import waves from '@/directive/waves'

  export default {
    name: 'supplierManager',
    // 水波文效果
    directives: {
      waves
    },
    data() {
      return {
        list: null,
        total: 0,
        listLoading: true,
        downloadLoading: false,
        // 查询参数
        listQuery: {
          status: 'ok',
          current: 1,
          size: 10
        },
        dialogStatus: 'create',
        dialogFormVisible: false,
        // 表单校验规则
        rules: {
          name: [
            { required: true, message: '供应商名称必输', trigger: 'blur' }
          ],
          contact: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
          number: [{ required: true, message: '联系人号码为空', trigger: 'blur' }],
          adress: [{ required: true, message: '联系地址不能为空', trigger: 'blur' }]
        },
        // 创建或修改供应商临时对象
        temp: {}
      }
    },
    // 页面加载完成后显示列表页
    created() {
      this.querySupplier()
      this.resetForm()
    },
    methods: {
      /**
       * 用户列表
       */
      queryResource() {
        this.listLoading = true
        queryResource(this.listQuery).then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },

      // 查询过滤器
      handleFilter() {
        this.listQuery.current = 1
        this.queryResource()
      },
      /**
       * 修改每页显示条数
       */
      handleSizeChange(val) {
        this.listQuery.size = val
        this.queryResource()
      },
      /**
       * 跳转到指定页
       */
      handleCurrentChange(val) {
        this.listQuery.current = val
        this.queryResource()
      },
      /**
       * 重置添加表单
       */
      resetForm() {
        this.temp = {
          id:'',
          code: '',
          type: '',
          name: '',
          url: '',
          description: '',
          method: ''
        }
      },
      /**
       * 弹出创建资源
       */
      handleCreate() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      /**
       * 创建用户
       */
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            createResource(this.temp).then(() => {
              this.dialogFormVisible = false
              this.$notify({
                title: '创建成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
              this.queryResource()
            })
          }
        })
      },
      /**
       * 弹出修改用户表单
       */
      handleUpdate(id) {
        this.listLoading = true
        getResource(id).then(response => {
          this.temp = response.data
          this.listLoading = false
          this.dialogStatus = 'edit'
          this.dialogFormVisible = true
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      /**
       * 修改用户信息
       */
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            updateResource(this.temp).then(() => {
              this.dialogFormVisible = false
              this.$notify({
                title: '编辑成功',
                message: '编辑成功',
                type: 'success',
                duration: 2000
              })
              this.temp.roleIds = []
              this.queryUser()
            })
          }
        })
      },
      /**
       * 删除用户
       */
      deleteData(id) {
        this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          center: true,
          type: 'warning'
        }).then(() => {
          deleteResource(id).then(() => {
            this.$notify({
              title: '删除成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.queryResource()
          })
        })
      },
      /**
       * 导出列表
       */
      handleDownload() {
        console.log('download')
      }
    }
  }
</script>

